<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-06-18
 * Time: 16:09
 */

namespace app\mall\controller;

use app\common\BaseLoginCustomer;
use app\exception\CustomException;
use app\mall\service\OrdersService;
use app\tools\Wxpay;
use think\Request;

class Orders extends BaseLoginCustomer
{
    private $ordersService;
    function __construct(Request $request = null, OrdersService $ordersService)
    {
        parent::__construct($request);
        $this->ordersService = $ordersService;
    }

// 订单信息
    function order_info()
    {
        $goodsId = $this->getParam('goods_id', 0);
        $num = $this->getParam('num', 0);

        $goods = $this->ordersService->goods([
            "num"=> $num,
            "goods_id"=> $goodsId,
            "customer_id"=> $this->userInfo['id'],
        ]);

        return success($goods);


    }

    // 生成订单

    function order_confirm(){

        $data = [
            "goods_id"=> $this->getParam("goods_id", 0),
            "num"=> $this->getParam("num", 0),
            "tel"=> $this->getParam("tel", 0),
            "address2"=> $this->getParam("address", 0),
            "customer_id"=> $this->userInfo['id']
        ];

        if(!$data['goods_id'] || !$data['num']){
            throw new CustomException("DATA_ERROR");
        }

        $data = $this->ordersService->confirm($data);
        if(!$data['use_money'])
        {
            $wxpay = new Wxpay();
            $result = $wxpay->unifieOrder([
                "out_trade_no"=> $data['serial_no'],
                "total"=> $data['total'],
                "openid"=> $this->userInfo['openid'],
            ], config('host').'/api/wxpay/notify3');

        }else{
            $result = [];
        }

        $result['o_id'] = $data['o_id'];
        return success($result);

    }

    function orderInfo()
    {
        $o_id = $this->getParam('o_id', 0);
        $data = $this->ordersService->getInfo($o_id);
        return success($data);
    }

    function myCoupon()
    {
        $customer_id = $this->userInfo['id'];
        $status = $this->getParam('status', 1);
        $data = $this->ordersService->myCoupon($customer_id, $status, $this->pageSize);
        return success($data);
    }

    function couponVerification()
    {
        $param = [
            'my_coupon_id'=> $this->getParam('my_coupon_id', 0),
            'u_id'=> $this->getParam('u_id', 0),
            'customer_id'=> $this->getParam('customer_id', 0)
        ];
        $data = $this->ordersService->couponVerification($param);
        return success($data);

    }



    function getCodeUrl()
    {
        $param = [
            'id'=> $this->getParam('my_coupon_id', 0),
            'uid'=> $this->userInfo['id']
        ];
//        dump($param);

        $data = $this->ordersService->getCodeUrl($param);
        return success(config('host').$data);
    }

    function lists()
    {
        $param = [
            'o.status'=> $this->getParam('status', 0),
            'o.customer_id'=> $this->getParam('u_id', 0)
        ];

        $data = $this->ordersService->lists($param);
        return success($data);

    }

    function orderDetail()
    {
        $param = [
            'id'=> $this->getParam('o_id', 0),
            'customer_id'=> $this->getParam('u_id', 0)
        ];

        if(!$param['id'])
        {
            throw new CustomException("DATA_ERROR");
        }

        $data = $this->ordersService->orderDetail($param);
        return success($data);

    }

    function evaluate()
    {
        $param = [
            'star'=> $this->getParam('star', 0),
            'eval'=> $this->getParam('eval', ''),
            'id'=> $this->getParam('o_id', 0),
        ];

        if(!$param['eval'] || !$param['id'])
        {
            throw new CustomException("DATA_ERROR");
        }
        $this->ordersService->evaluate($param, $this->userInfo['id']);
        return success([]);

    }

    function reward()
    {
        $param = [
            'id'=> $this->getParam('o_id', 0)
        ];
        if(!$param['id'])
        {
            throw new CustomException("DATA_ERROR");
        }
        $data = $this->ordersService->reward($param);
        return success($data);
    }

    function getPrize()
    {
        $param = [
            'customer_id'=> $this->getParam('u_id', 0),
            'o_id'=> $this->getParam('o_id', 0),
            'type'=> $this->getParam('type', 1)
        ];
        $data = $this->ordersService->getPrize($param);
        return success($data);
    }

    function redPacket()
    {
        $param = [
            'customer_id'=> $this->userInfo['id']
        ];
        $data = $this->ordersService->redPacket($param);
        return success($data);
    }

    function refund()
    {
        $param = [
            'id'=> $this->getParam('o_id', 0),
            'customer_id'=> $this->getParam('u_id', 0),
            'type'=> $this->getParam('type', 1)
        ];
        if(!$param['id'])
        {
            throw new CustomException("DATA_ERROR");
        }
        $data = $this->ordersService->refund($param);
        return success([]);
    }
}